;
; SYSLIB Module Name:  SPHL5D
; Author:  Richard Conn
; SYSLIB Version Number:  2.0
; Module Version Number:  1.0
; Module Entry Points:
;	PHL5DC		PHLDC
; Module External References:
;	COUT
;

;
;  PHL5DC --
;	PRINT HL AS 5 DECIMAL CHARACTERS
;
	EXT	COUT

PUTRG	MACRO
	PUSH	B	; SAVE BC, DE, HL
	PUSH	D
	PUSH	H
	ENDM
GETRG	MACRO
	POP	H	; RESTORE HL, DE, BC
	POP	D
	POP	B
	ENDM


PHL5DC::
	PUSH	PSW	; SAVE ALL REGS
	PUTRG
	XRA	A	; A=0
	STA	LSFLG0	; TURN OFF LEADING <SP>
	JMP	PHDC
;
;  PHLDC --
;	PRINT HL AS DECIMAL CHARACTERS W/LEADING SPACES IN 5-CHAR FIELD
;
PHLDC::
	PUSH	PSW	; SAVE ALL REGS
	PUTRG
	MVI	A,1	; A=1
	STA	LSFLG0	; TURN ON LEADING <SP>
;  PRINT HL USING LEADING <SP> FLAG LSFLG0
PHDC:	LXI	D,10000	; PRINT 10000'S
	CALL	PHDC1
	LXI	D,1000	; PRINT 1000'S
	CALL	PHDC1
	LXI	D,100	; PRINT 100'S
	CALL	PHDC1
	LXI	D,10	; PRINT 10'S
	CALL	PHDC1
	MOV	A,L	; PRINT 1'S
	ADI	'0'	; CONVERT TO ASCII
	CALL	COUT
	GETRG		; RESTORE ALL REGS
	POP	PSW
	RET

;  BUFFER
LSFLG0:	DS	1	; LEADING <SP> FLAG
	
;  DIVIDE HL BY DE AND PRINT QUOTIENT WITH LEADING <SP>S
PHDC1:	MVI	C,0	; SET COUNT
PHDC2:	MOV	A,L	; SUB E FROM L
	SUB	E
	MOV	L,A	; RESULT IN L
	MOV	A,H	; SUB D FROM H W/BORROW
	SBB	D
	MOV	H,A	; RESULT IN H
	JC	PHDC3	; DONE IF CARRY SET (FURTHER BORROW)
	INR	C	; INCR COUNT
	JMP	PHDC2
PHDC3:	MOV	A,L	; ADD E TO L
	ADD	E
	MOV	L,A	; RESULT IN L
	MOV	A,H	; ADD D TO H W/CARRY
	ADC	D
	MOV	H,A	; RESULT IN H
	MOV	A,C	; GET RESULT
	ORA	A	; CHECK FOR ZERO
	JNZ	PHDC4
	LDA	LSFLG0	; CHECK FOR LEADING <SP>
	ORA	A	; PRINT VALUE IF NOT (A=0)
	JZ	PHDC4
	MVI	A,' '	; PRINT <SP>
	CALL	COUT
	RET
PHDC4:	XRA	A	; TURN OFF LEADING <SP>
	STA	LSFLG0
	MOV	A,C	; GET VALUE
	ADI	'0'	; CONVERT TO ASCII
	CALL	COUT
	RET

	END
